package com.xydtech.ecard.busi.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReprotInfo;
import com.xydtech.ecard.busi.domain.EcardApiBusiReprotRecord;
import com.xydtech.ecard.busi.mapper.EcardApiBusiReportConfigMapper;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片业务填报类型配置Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-25 下午1:42
 */
@Service
public class EcardApiBusiReportConfigServiceImpl implements IEcardApiBusiReportConfigService {

    @Autowired
    private EcardApiBusiReportConfigMapper ecardBusiReportConfigMapper;

    /**
     * 查询电子名片业务填报类型配置
     * @param id 电子名片业务填报类型配置主键
     * @return
     */
    @Override
    public EcardApiBusiReportConfig getReportConfigById(Long id) {
        return ecardBusiReportConfigMapper.getReportConfigById(id);
    }

    /**
     * 查询法人机构下业务填报类型配置列表
     * @param ecardBusiReportConfig
     * @return
     */
    @Override
    public List<EcardApiBusiReportConfig> getReportConfigList(EcardApiBusiReportConfig ecardBusiReportConfig) {
        return ecardBusiReportConfigMapper.getReportConfigList(ecardBusiReportConfig);
    }

    /**
     * 条件查询查询登录人名下的业务填报记录接口
     * @param busiReprotRecord
     * @return
     */
    @Override
    public List<EcardApiBusiReprotRecord> getMyBusiReportList(EcardApiBusiReprotRecord busiReprotRecord) {
        return ecardBusiReportConfigMapper.getMyBusiReportList(busiReprotRecord);
    }

    /**
     * 条件查询查询登录人名下的业务列表接口
     * @param busiReprotRecord
     * @return
     */
    @Override
    public List<EcardApiBusiReprotRecord> getMyBusiReportInfo(EcardApiBusiReprotRecord busiReprotRecord, String salt) {
        List<EcardApiBusiReprotRecord> busiReprotRecords = ecardBusiReportConfigMapper.getMyBusiReportInfo(busiReprotRecord);
        if (busiReprotRecords.size() > 0) {
            for (EcardApiBusiReprotRecord reprotRecord : busiReprotRecords) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(reprotRecord.getCustPhone())) {
                        reprotRecord.setCustPhone(AESEncryptionUtils.decrypt(reprotRecord.getCustPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReprotRecords;
    }

    /**
     * 条件查询查询登录人名下的业务详情接口
     * @param busiReprotInfo
     * @return
     */
    @Override
    public EcardApiBusiReprotInfo getMyBusiReportDetail(EcardApiBusiReprotInfo busiReprotInfo, String salt) {
        /**
         * 根据业务类型查询业务详情
         */
        Integer busiType = busiReprotInfo.getBusiType();//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
        EcardApiBusiReprotInfo ecardBusiReprotInfo = new EcardApiBusiReprotInfo();
        try {
            if (busiType == 1) {
                ecardBusiReprotInfo = ecardBusiReportConfigMapper.getMyBusiReportDetailByPerson(busiReprotInfo);
                if (ecardBusiReprotInfo != null) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getPerCustPhone())) {
                        ecardBusiReprotInfo.setPerCustPhone(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getPerCustIdcard())) {
                        ecardBusiReprotInfo.setPerCustIdcard(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getPerCustIdcard(), salt));
                    }
                }
            } else if (busiType == 2) {
                ecardBusiReprotInfo = ecardBusiReportConfigMapper.getMyBusiReportDetailByCard(busiReprotInfo);
                if (ecardBusiReprotInfo != null) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getPerCustPhone())) {
                        ecardBusiReprotInfo.setPerCustPhone(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getPerCustIdcard())) {
                        ecardBusiReprotInfo.setPerCustIdcard(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getPerCustIdcard(), salt));
                    }
                }
            } else if (busiType == 3) {
                ecardBusiReprotInfo = ecardBusiReportConfigMapper.getMyBusiReportDetailByPublic(busiReprotInfo);
                if (ecardBusiReprotInfo != null) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getPubPhone())) {
                        ecardBusiReprotInfo.setPubPhone(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getPubPhone(), salt));
                    }
                }
            } else if (busiType == 4) {
                ecardBusiReprotInfo = ecardBusiReportConfigMapper.getMyBusiReportDetailByOther(busiReprotInfo);
                if (ecardBusiReprotInfo != null) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getOthCustPhone())) {
                        ecardBusiReprotInfo.setOthCustPhone(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getOthCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(ecardBusiReprotInfo.getOthCustIdcard())) {
                        ecardBusiReprotInfo.setOthCustIdcard(AESEncryptionUtils.decrypt(ecardBusiReprotInfo.getOthCustIdcard(), salt));
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReprotInfo;
    }
}
